package com.sky.controller.user;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.result.Result;
import com.sky.service.DishFlavorService;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController("userDishController")
@RequestMapping("/user/dish")
@Api(tags = "C端_菜品查询")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @ApiOperation("根据ID查询菜品")
    @GetMapping("/list")
    public Result<List<DishVO>> list(Long categoryId){
        Dish dish = new Dish();
        //当前分类在售的商品（没有口味）
        List<Dish> list = dishService.lambdaQuery()
                .eq(Dish::getCategoryId, categoryId)
                .eq(Dish::getStatus, StatusConstant.ENABLE)
                .list();


        List<DishVO> voList = list.stream().map((item) -> {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(item, dishVO);
            List<DishFlavor> dishFlavorList = dishFlavorService.lambdaQuery()
                    .eq(DishFlavor::getDishId, dishVO.getId())
                    .list();
            dishVO.setFlavors(dishFlavorList);
            return dishVO;
        }).collect(Collectors.toList());


        return Result.success(voList);

    }
}
